var fs = require('fs');
var path = require('path');
var src = './assetslist/'; //存储资源列表路径
var list = [];
var config = require("./Config")
var param =  config.inputparam;
var mkdirSync = function (path) {
    try {
        fs.mkdirSync(path);
    } catch (e) {
        if (e.code != 'EEXIST') throw e;
    }
}

function isDir(dir) {
    var dirtmp = path.join(dir, )
    var stat = fs.statSync(dir);
    return stat.isDirectory();
}

function isListRes(dir) {
    if (!isDir(dir)) {
        dir = dir.substring(dir.indexOf('.'));
        var judge = dir === '.fire' || dir === '.prefab' || dir === '.mp3';
        return judge;
    }
    return false;
}

function readDir(dir, mod) {
    if (!isDir(dir)) {
        return;
    }
    var subpaths = fs.readdirSync(dir),
        subpath;
    for (var i = 0; i < subpaths.length; ++i) {
        if (subpaths[i][0] === '.') {
            continue;
        }
        subpath = path.join(dir, subpaths[i]);
        stat = fs.statSync(subpath);
        if (stat.isDirectory()) {
            readDir(subpath, mod);
        } else if (stat.isFile()) {
            if (isListRes(subpath)) {
                var pathTmp = subpath.substring(subpath.lastIndexOf("/assets/") + 8);
                pathTmp = pathTmp.replace(/\\/g, '/');
                var obj = {
                    module: mod,
                    path: pathTmp
                }
                // console.log("module is " + mod);
                // console.log("pathTmp is " + pathTmp);
                list.push(obj);
            }
        }
    }

}

if (param) {

    function clearDirs(dirname) {
        if (fs.existsSync(dirname)) {
            var files = fs.readdirSync(dirname);
            files.forEach(function (file, index) {
                var curPath = dirname + "/" + file;
                if (fs.statSync(curPath).isDirectory()) {
                    fs.rmdirSync(dirname);
                    // console.log("文件夹");
                } else {
                    // console.log("删除文件", file);
                    fs.unlinkSync(curPath, function (err) {
                        if (err) throw err;
                    });
                }
            })
        }
    }

    function mkdirsSync(dirname) {
        if (fs.existsSync(dirname)) {
            return true;
        } else {
            if (mkdirsSync(path.dirname(dirname))) {
                fs.mkdirSync(dirname);
                return true;
            }
        }
    }
    var writeFileInfo = function (file, fullStr) {
        mkdirsSync(src);
        fs.appendFileSync(file, fullStr);
    }
    //清空文件目录
    clearDirs(src);
    var moduleTmp = '';
    for (var i = 0; i < param.length; i++) {
        var include = param[i].include;
        if (include) {
            var mod = param[i].name;
            var filepath = param[i].path;
            var srcpath = src + mod + 'list.json';
            if (i == param.length - 1) {
                readDir(filepath, mod);
                var listStr = JSON.stringify(list);
                writeFileInfo(srcpath, listStr);
                list = [];
                break;
            }
            moduleTmp = param[i + 1].name;
            if (moduleTmp === mod) {
                readDir(filepath, mod);
            } else {
                readDir(filepath, mod);
                var listSec = JSON.stringify(list);
                writeFileInfo(srcpath, listSec);
                list = [];
            }
        }
    }
}